package net.pimpas.web.jsf.components.datepicker;

import javax.faces.component.html.HtmlInputText;
import javax.faces.context.FacesContext;

import net.pimpas.web.jsf.util.ComponentInformation;

public class UIDatePicker extends HtmlInputText {


	public static final String COMPONENT_TYPE			= ComponentInformation.COMPONENT_PREFIX_TYPE+".datepicker";
	public static final String COMPONENT_FAMILY			= ComponentInformation.COMPONENT_PREFIX_FAMILY+".datepicker";
	public static final String COMPONENT_RENDER_TYPE	= ComponentInformation.COMPONENT_PREFIX_RENDER+".datepicker";
	public static final String DEFAULT_JS = 			"calendar.js";
	public static final String DEFAULT_SETUP_JS = 		"calendar-setup.js";
	public static final Integer DEFAULT_FIRST_DOW		= 1;
	public static final Boolean DEFAULT_SHOW_WN			= Boolean.FALSE;
	public static final String DEFAULT_DATE_FORMAT 		= "%m/%d/%Y";
	public static final String[] DEFAULT_THEMES			= {"calendar-blue",
															"calendar-blue2",
															"calendar-brown",
															"calendar-green",
															"calendar-tas",
															"calendar-win2k-1",
															"calendar-win2k-2",
															"calendar-win2k-cold-1",
															"calendar-win2k-cold-2"};
	
	public static final String DATE_PICKER_STYLE			= "datePicker";
	public static final String DATE_PICKER_BUTTON_ID		= "_datePickerButton";
	public static final String DATE_PICKER_BUTTON_STYLE		= "datePickerButtonStyle";
	public static final Integer DEFAULT_THEME_INDEX			= 0;
	
	private Integer firstDayOfWeek;
	private Boolean showWeekNumbers;
	private String dateFormat;
	private String onSelected;
	private String onClosed;
	private String theme;
	
	@Override
	public Object saveState(FacesContext context) {
		Object[] ob = new Object[7];
		ob[0] = super.saveState(context);
		ob[1] = this.getFirstDayOfWeek();
		ob[2] = this.getShowWeekNumbers();
		ob[3] = this.getDateFormat();
		ob[4] = this.getOnSelected();
		ob[5] = this.getOnClosed();
		ob[6] = this.getTheme();
		
		return ob;
	}
	
	@Override
	public void restoreState(FacesContext context, Object state) {
		Object[] ob = (Object[]) state;
		super.restoreState(context, ob[0]);
		this.setFirstDayOfWeek((Integer) ob[1]);
		this.setShowWeekNumbers((Boolean) ob[2]);
		this.setDateFormat((String) ob[3]);
		this.setOnSelected((String) ob[4]);
		this.setOnClosed((String) ob[5]);
		this.setTheme((String) ob[6]);
	}
	
	@Override
	public boolean getRendersChildren() {
		return true;
	}
	
	public static Boolean checkTheme(String theme) {
		for(int i=0;i<UIDatePicker.DEFAULT_THEMES.length;i++)
			if(UIDatePicker.DEFAULT_THEMES[i].equals(theme)) return Boolean.TRUE;
		return Boolean.FALSE;
	}

	@Override
	public String getRendererType() {
		return COMPONENT_RENDER_TYPE;
	}

	@Override
	public String getFamily() {
		return COMPONENT_FAMILY;
	}

	public String getDateFormat() {
		return dateFormat;
	}

	public void setDateFormat(String dateFormat) {
		this.dateFormat = dateFormat;
	}

	public Integer getFirstDayOfWeek() {
		return firstDayOfWeek;
	}

	public void setFirstDayOfWeek(Integer firstDayOfWeek) {
		this.firstDayOfWeek = firstDayOfWeek;
	}
	public String getOnClosed() {
		return onClosed;
	}

	public void setOnClosed(String onClosed) {
		this.onClosed = onClosed;
	}

	public String getOnSelected() {
		return onSelected;
	}

	public void setOnSelected(String onSelected) {
		this.onSelected = onSelected;
	}
	public Boolean getShowWeekNumbers() {
		return showWeekNumbers;
	}

	public void setShowWeekNumbers(Boolean showWeekNumbers) {
		this.showWeekNumbers = showWeekNumbers;
	}

	public String getTheme() {
		return theme;
	}

	public void setTheme(String theme) {
		this.theme = theme;
	}
}